/**
 * localStorage辅助工具
 * 提供类型安全的localStorage操作
 */

import LocalStorage from './localStorage';

const storage = new LocalStorage();

/** 学校信息 */
export interface SelectedSchool {
  name: string;
  id: number;
}

/** 班级信息 */
export interface SelectedClass {
  class_name: string;
  class_code: string;
}

/**
 * 获取选中的学校
 */
export const getSelectedSchool = (): SelectedSchool | null => {
  try {
    const data = storage.getItem('selected_school');
    if (!data) return null;
    return JSON.parse(data) as SelectedSchool;
  } catch {
    return null;
  }
};

/**
 * 设置选中的学校
 */
export const setSelectedSchool = (school: SelectedSchool): void => {
  storage.setItem('selected_school', JSON.stringify(school));
};

/**
 * 获取选中的班级
 */
export const getSelectedClass = (): SelectedClass | null => {
  try {
    const data = storage.getItem('selected_class');
    if (!data) return null;
    return JSON.parse(data) as SelectedClass;
  } catch {
    return null;
  }
};

/**
 * 设置选中的班级
 */
export const setSelectedClass = (classInfo: SelectedClass): void => {
  storage.setItem('selected_class', JSON.stringify(classInfo));
};

/**
 * 检查是否使用课程缓存
 */
export const getUseCourseCache = (): boolean => {
  const value = storage.getItem('use_course_cache');
  return value === 'true';
};

/**
 * 设置是否使用课程缓存
 */
export const setUseCourseCache = (use: boolean): void => {
  storage.setItem('use_course_cache', use ? 'true' : 'false');
};

/**
 * 清除所有选择
 */
export const clearAllSelections = (): void => {
  storage.removeItem('selected_school');
  storage.removeItem('selected_class');
  storage.removeItem('use_course_cache');
};
